package com.ycx.ycxmybatismanagement.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@RestController
public class CookieContriller {
    @GetMapping("/setcookie")
    public String setcooke(HttpServletResponse response){
        Cookie cookieId = new Cookie("id", "1");
        Cookie cookieName = new Cookie("name", "zhangsan");
        response.addCookie(cookieId);
        response.addCookie(cookieName);
        return "success";
    }
    @GetMapping("/order")
    public String orders(HttpServletRequest request){
        Integer userId = 0;
        String username = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            String value = cookie.getValue();
            if(Objects.equals(name,"id")){
                userId = Integer.parseInt(value);
            }
            if (Objects.equals(value,"name")){
                username = value;
            }
        }
        return "success";
    }

}
